package cn.xyfmzmy.adapter.spring.etcd;

import io.etcd.jetcd.Client;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisKeyValueAdapter;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * Etcd替换Redis配置类
 */
@Configuration
public class EtcdRedisConfiguration {

    // 注册etcd客户端
    @Bean
    public Client etcdClient(EtcdProperties etcdProperties){
        return Client.builder().endpoints(etcdProperties.getUrl()).build();
    }

    // 注册EtcdValueOperations,EtcdValueOperations里定义了用etcd替换redis相关api的底层实现
    @Bean
    public EtcdValueOperations etcdValueOperations(Client client){
        return new EtcdValueOperations(client);
    }

    // 使用EtcdRedisTemplateAdapter替换原本的RedisTemplate
    @Bean
    public RedisTemplate<String,String> redisTemplate(EtcdValueOperations operations){
        return new EtcdRedisTemplateAdapter(operations);
    }

    // 有点复杂，也是用于跳过检查
    @Bean
    public RedisKeyValueAdapter redisKeyValueAdapter(){
        return new VoidRedisKeyValueAdapter();
    }

}
